﻿using MediatR;
using S.Parturition.Domain;
using S.Parturition.Read.API.Application.Command;
using S.Parturition.Read.API.Dto;

namespace S.Parturition.Read.API.Application.CommandHandlers
{
    public class GetEarTagListHandler : IRequestHandler<GetEarTagListCommand, List<EarTagDto>>
    {

        private readonly MyDbContext dbContext;

        public GetEarTagListHandler(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public async Task<List<EarTagDto>> Handle(GetEarTagListCommand request, CancellationToken cancellationToken)
        {
            //获取到种母分娩信息
            var childList = (from p in dbContext.ParturitionModel.Where(x => x.SheepStatus == false)
                             join l in dbContext.LambRegistrationModel on p.EarTag equals l.MotherEar
                             select new
                             {
                                 ParturitionId = p.ParturitionId,
                                 EarTag = p.EarTag,
                             }).Distinct().ToList();
            //将Id与耳号赋值
            List<EarTagDto> children = childList.Select(x => new EarTagDto
            {
                Value = x.ParturitionId,
                Label = x.EarTag,
            }).ToList();

            return children;
        }
    }
}
